package com.weixing.mall.provider.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.base.constant.DatePattern;
import com.weixing.mall.provider.mapper.LoginLogMapper;
import com.weixing.mall.provider.model.domain.LoginLog;
import com.weixing.mall.provider.model.query.SysLoginLogQuery;
import com.weixing.mall.provider.model.vo.sys.SysLoginLogVo;
import com.weixing.mall.provider.service.ILoginLogService;
import jodd.util.StringUtil;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统模块-登录日志表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements ILoginLogService {

    @Override
    public IPage<LoginLog> selectAllOfPage(Page initMpPage, SysLoginLogQuery query) {
        QueryWrapper<LoginLog> wrapper = new QueryWrapper<LoginLog>();
        if (StringUtil.isNotBlank(query.getUserName())) {
            wrapper.like("user_name", query.getUserName());
        }
        if (StringUtil.isNotBlank(query.getIpLoc())) {
            wrapper.like("ip_loc", query.getIpLoc());
        }
        if (StringUtil.isNotBlank(query.getIpAddr())) {
            wrapper.like("ip_addr", query.getIpAddr());
        }
        if (StringUtil.isNotBlank(query.getStartTime())) {
            wrapper.ge("created_time", DateUtil.parse(query.getStartTime() + " 00:00:00", DatePattern.NORM_DATETIME_PATTERN));
        }
        if (StringUtil.isNotBlank(query.getEndTime())) {
            wrapper.le("created_time", DateUtil.parse(query.getEndTime() + " 23:59:59", DatePattern.NORM_DATETIME_PATTERN));
        }
        wrapper.orderByDesc("id");

        return page(initMpPage, wrapper);
    }

    @Override
    public void batchDelete(SysLoginLogQuery sysAccessLogQuery) {
        baseMapper.batchDelete(sysAccessLogQuery);
    }

    @Override
    public IPage<SysLoginLogVo> selectOfPage(Page page) {
        return baseMapper.selectOfPage(page);
    }

}
